﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments
{
    class EVN
    {
        public StringBuilder event_type_code { get; set; }
      //  public int event_id_code { get; set; }

        public StringBuilder date_of_event { get; set; }
        public bool required { get; set; }

        public EVN()
        {
            event_type_code = new StringBuilder();
            date_of_event = new StringBuilder();
        }
        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i,x=0;

            for (i = temp; i < source.Length; i++)
            {
                if (source[i] == '|')
                {
                    if (x == 2)
                        return i;
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else if (x == 1)
                {
                    if (source[i] != '|') // pasing event code paramter
                    {
                        event_type_code.Insert(event_type_code.Length, source[i]);                       
                    }
                    else
                    {
                        x++;
                        event_type_code.Capacity = event_type_code.Length;
                        break;
                    }
                }
                else if (x == 2) // pasing data parameter
                {
                    if (check(source, i) != null)
                    {
                        event_type_code.Capacity = event_type_code.Length;
                        date_of_event.Capacity = date_of_event.Length;
                        return i - 1;
                    }
                   else if (source[i] != '|')
                    {
                        date_of_event.Insert(date_of_event.Length, source[i]);
                    }                 
                }
            }

            return i;
        }
    }
}
